<?php
/**
 * Класс для обработки формы регистрации
 *
 * @author kingop
 */
class FormProcessor_Category extends FormProcessor {
    
    protected $db = null;               // объект для соединения с базой данных
    public $user = null;                // объект класса DatabaseObject_User
    protected $_validateOnly = false;   // флаг, указывающий тип запроса: true = ajax
    
    public function __construct($db, $id = 0) {
        parent::__construct();
        $this->db = $db;
        $this->category = new DatabaseObject_Categories($this->db);
         if ($id!='0'){
        $this->category->load($id);
        $this->id = $this->category->getId();
        $this->name = $this->category->name;
        $this->title = $this->category->title;
        $this->descr = $this->category->descr;
        $this->keywords = $this->category->keywords;
        $this->alias = $this->category->alias;
        $this->content = $this->category->content;
        $this->parent_id = $this->category->parent_id;
        $this->count = $this->category->count;
         $this->level = $this->category->level;
        }
        
    }
    
    public function process(Zend_Controller_Request_Abstract $request) {
        // проверим название
        $this->name = trim($request->getPost('name'));

        if (strlen($this->name) == 0)
            $this->addError('name', 'Вы должны указать название');
        else
            $this->category->name = $this->name;
        
         // проверим alias (url)
        if ($request->getPost('alias')!='')
            $this->alias = trim($request->getPost('alias')); 
        else 
            $this->alias = parent::rewrite($this->name);
        $this->category->alias = $this->alias;
        
        // проверим meta тег title 
        if ($request->getPost('title'))
            $this->title = trim($request->getPost('title')); 
        else 
            $this->title = $this->name;
         $this->category->title = $this->title;
         
         // проверим meta тег description
        $this->descr = trim($request->getPost('descr'));
        $this->category->descr = $this->descr;
        
         // проверим meta тег keywords
        $this->keywords = trim($request->getPost('keywords'));
        $this->category->keywords = $this->keywords; 
          
        // проверим контент
        $this->content = trim($request->getPost('content'));
        $this->category->content = $this->content;
        
         // проверим родительская категория
        $this->parent_id = trim($request->getPost('parent_id'));
        $te = new DatabaseObject_Categories($this->db);
        $te->load($this->parent_id);
        $this->category->level=$te->level+1;
        $this->category->parent_id = $this->parent_id;
         
        if ($id)
        $this->category->count = $this->count;
        else
             $this->category->count = 0;
        
        // если не было ошибок, сохраним пользователя
        if (!$this->_validateOnly && !$this->hasError()) {
            $this->category->save();
        }
        
        return !$this->hasError();
    }
}

?>
